// --------------------------------------------------------------------------------------------------------------------- 
// <copyright file="WorkItemChangedEndpoint.cs" company="Scrum for Team System">
//   None
// </copyright>
// <summary>
//   Defines the WorkItemChangedEndpoint type.
// </summary>
// ---------------------------------------------------------------------------------------------------------------------

namespace ScrumForTeamSystem.EventService.WebService
{
    using System.Web.Services;
    using System.Web.Services.Protocols;

    using Properties;

    /// <summary>
    /// Web Service Endpoint for the <see cref="WorkItemChangedEvent"/>
    /// </summary>
    /// <remarks>
    /// To Register this Notification Endpoint you will need to use the <see cref="http://blogs.msdn.com/psheill/archive/2006/02/01/522386.aspx">BisSubscribe.exe</see> tool. 
    /// For more information about Team Foundation Server Notifications <see cref="http://blogs.msdn.com/buckh/archive/2006/04/10/event_filtering.aspx"/>
    /// </remarks>
    [WebService(Namespace = "http://schemas.ScrumForTeamSystem.com/Notification/Extensibility",
            Description = "")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class WorkItemChangedEndpoint : EndpointBase
    {
        /// <summary>
        /// Endpoint required to accept incoming <see cref="WorkItemChangedEvent"/> notifications
        /// </summary>
        /// <param name="eventXml">
        /// Serialized version of the <see cref="WorkItemChangedEvent"/>
        /// </param>
        /// <param name="tfsIdentityXml">
        /// Serialized version of <see cref="TFSIdentity"/>
        /// </param>
        [WebMethod]
        [SoapDocumentMethod("http://schemas.microsoft.com/TeamFoundation/2005/06/Services/Notification/03/Notify",
                RequestNamespace = "http://schemas.microsoft.com/TeamFoundation/2005/06/Services/Notification/03")]
        public void Notify(string eventXml, string tfsIdentityXml)
        {
            System.Diagnostics.Debug.WriteLine(eventXml);

            if (!ServiceControl.IsListening)
            {
                return;
            }

            var workItemChangedEvent = CreateInstance<WorkItemChangedEvent>(eventXml);
            var tfsIdentity = CreateInstance<TFSIdentity>(tfsIdentityXml);

            if (Settings.Default.ExcludedProjects.Contains(workItemChangedEvent.PortfolioProject))
            {
                return;
            }

            this.TfsEventQueueService.Enqueue(workItemChangedEvent, tfsIdentity);
        }
    }
}